/* 
 @description: 网站设置JS

 @author: 韦淇霰

 @update: 2021年9月12日20:55:35
 */
 const back = document.querySelector('.back'),
  submit = document.getElementById('submit'),
  closes = document.getElementById('closes'),
  logo = document.getElementById('logo'),
  wz_content1 = document.getElementById('wz_content1'),
  wz_content2 = document.getElementById('wz_content2'),
  wz_bq = document.getElementById('wz_bq'),
  wz_rx= document.getElementById('wz_rx'),
  form = document.getElementById('tab'),
  btn = document.querySelector('.btn');
  getList();
  const imgCon = document.getElementById('imgCon');
  const img = document.getElementById('img');
  const imgCon1 = document.getElementById('imgCon1');
  const img1 = document.getElementById('img1');
  imgCon.style.display = 'block';
  imgCon1.style.display = 'block';
  console.log(img,img1)
 let text 
  //渲染
  const src= `http://127.0.0.1:7001`
  function getList() {

      $.ajax({
      url: `http://127.0.0.1:7001/api/setUp`,
      method: "get",
      success: function(res) {
            console.log(res.data[0]);
            logo.text = res.data[0].logo;
            wz_content1.value = res.data[0].about;
            wz_content2.value = res.data[0].privacy;
            wz_bq.value = res.data[0].copyright;
            wz_rx.text = res.data[0].hotline;
            img1.src=`${src}/${res.data[0].logo}`;
            img.src=`${src}/${res.data[0].hotline}`;
         }
      }
  )}
  let ida = 1;
  //修改
  function EditCa(data,id){
      console.log(data)
      $.ajax({
      url:`http://127.0.0.1:7001/api/setUp/${id}`,
      method:"put",
      data,
      success:function(res){
        getList();
        console.log(res);
          if(res.code === 0){
          window.location.reload();
          }else{
              alert(res.msg);
          }
      }
  })
  }
  //修改按钮
btn.onclick = function(){

    confirm('确定提交吗');
        picture();
        picture_a();
        picture_aa();
        picture_bb();
        console.log(logo.text);
        console.log(wz_rx.text);
    EditCa({
        logo : logo.text,
        about : wz_content1.value,
        privacy : wz_content2.value,
        copyright : wz_bq.value,
        hotline : wz_rx.text
    },ida)
   
    // getInfo(1);
  }
  
   //上传照片
//    logo.addEventListener('change',function(){
//       const up = document.getElementById('tab');
//       console.log(up);
//       const formData = new FormData(up);
//       var ll=formData.get('logo')
//       picture(ll);
//    })
// let text;
  function picture(){
    console.log(111);
      const img_file = document.getElementById('logo');
      const formData = new FormData();
      const fileObj = img_file.files[0];
      formData.append("classIcon",fileObj);
    //   console.log(formData);
      $.ajax({
          url:`http://127.0.0.1:7001/api/picture`,
          type :'post',
          data : formData,
          async :false,
          cache: false,
          processData :false,
          contentType : false,
          success : function(data){
              if(data.code === 0){
                  console.log(data)
                  logo.text = data.data;
                //   console.log(this);
                  localStorage.setItem('data',data.data);
                //   console.log(localStorage.getItem('data'));
              }
             
          }
            
      })
     
  }
  //传轮播图路径
  function picture_a(){
     const data = localStorage.getItem('data');
     console.log(data);
      $.ajax({
          url:`http://127.0.0.1:7001/api/setUp`,
          method:"post",
          data:data,
          success:function(res){
              if(res.code === 0){
                  console.log(data);
              }else{
                  alert(res.msg);
              }
          }
      })
  }
  function picture_aa(){
    console.log(222);
      const img_file1 = document.getElementById('wz_rx');
      console.log(img_file1);
      const formData1 = new FormData();
      const fileObj1 = img_file1.files[0];
      formData1.append("classIcon",fileObj1);
      $.ajax({
          url:`http://127.0.0.1:7001/api/picture`,
          type :'post',
          data : formData1,
          async :false,
          cache: false,
          processData :false,
          contentType : false,
          success : function(data){
              if(data.code === 0){
                //   console.log(data)
                wz_rx.text = data.data;
                  localStorage.setItem('data1',data.data);
              }
             
          }
            
      })
     
  }

  //传轮播图路径
  function picture_bb(){
     const data1 = localStorage.getItem('data1');
    //  console.log(data1);
      $.ajax({
          url:`http://127.0.0.1:7001/api/setUp`,
          method:"post",
          data:data1,
          success:function(res){
              if(res.code === 0){
                //   console.log(data1);
              }else{
                  alert(res.msg);
              }
          }
      })
  }
  // 选择的图片预览在页面上

//   imgCon.style.display = 'none';
  let imgUrll = '';
  wz_rx.onchange = function (e) {
      const fileList = e.target.files;
    //   console.log(fileList);
      if (fileList.length) {
          imgCon.style.display = 'block';
          imgUrl = window.URL.createObjectURL(fileList[0]);
          img.src = imgUrl;
         //  console.log(imgUrl);
      }
  }

//   imgCon1.style.display = 'none';
  let imgU = '';
  logo.onchange = function (e) {
      const fileList1 = e.target.files;
      console.log(fileList1);
      if (fileList1.length) {
          imgCon1.style.display = 'block';
          imgU = window.URL.createObjectURL(fileList1[0]);
          img1.src = imgU;
         //  console.log(imgUrl);
      }
  }